$input v_fragPos, v_normal, v_texcoord0


#include "../../bgfx/examples/common/common.sh"


SAMPLER2D(s_texColor,  0);
SAMPLER2D(s_texNormal, 1);

uniform vec4 u_lightPos;
uniform vec4 u_lightColor;
uniform vec4 u_controlBlinnphong; //x:doBlinnPhongDiffuse y:doBlinnPhongSpecular z:doBlinnPhongAmbient

struct Light {
	vec3 color;
    vec4 position;
    vec3 ambient;
    vec3 diffuse;
    vec3 specular;
};

void main()
{
	Light light;
	light.position = mul(u_view, u_lightPos);
	light.color = u_lightColor.xyz;
	light.ambient = vec3(0.1f, 0.1f, 0.1f);
	light.diffuse = vec3(0.5f, 0.5f, 0.5f);
	light.specular = vec3 (1.0f, 1.0f, 1.0f);
	float shininess = 16.0f;
	
	vec4 materialColor = toLinear(texture2D(s_texColor, vec2(v_texcoord0.x, 1.0f - v_texcoord0.y)));

	vec3 normal = normalize(v_normal);

	// ambient
    vec3 ambient = light.ambient * materialColor.xyz * u_controlBlinnphong.z;

	//diffuse
    vec3 lightDir = normalize(light.position.xyz - v_fragPos);
    float diff = max(dot(normal, lightDir), 0.0);
    vec3 diffuse = light.diffuse * diff * materialColor.xyz * u_controlBlinnphong.x;  

	// specular
	vec3 view = normalize(-v_fragPos);
	vec3 h = normalize(view + lightDir);
	float spec = pow(max(dot(normal,h), 0.0), shininess); //api:step  prevent specular at back
	vec3 specular = light.specular * spec * light.color * u_controlBlinnphong.y;  

	gl_FragColor.xyz =ambient + diffuse + specular;
	gl_FragColor.w = 1.0;
	//gl_FragColor = vec4_splat(0.0);
	gl_FragColor = toGamma(gl_FragColor);
}
